<template>
  <el-dialog :visible.sync="show" :title="addressItem.id?'修改地址':'新增地址'" append-to-body>
    <div>
      <AddressForm :addressForm.sync="addressItem" ref="editRef" v-bind="$attrs"></AddressForm>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="show=false">取消</el-button>
      <el-button size="small" type="primary" @click="confirmSelect">确定</el-button>
    </span>
  </el-dialog>
</template>

<script lang="ts">
import {Component, Prop, PropSync, Watch, Vue} from "vue-property-decorator";
import AddressForm from "@/components/AddressForm.vue";
import {ElForm} from "element-ui/types/form";

@Component({
  components: {AddressForm}
})
export default class Address extends Vue{
  @Prop() addressItem!:any
  @PropSync('isVisible') show!:boolean;

  @Watch('show')
  WatchShow(val){
    val && this.$refs.editRef && (this.$refs.editRef as ElForm).clearValidate()
  }

  async confirmSelect(){
    const valid = await (this.$refs.editRef as ElForm).validate();
    if(!valid) return ;
    this.$api.address.editAddressApi(this.addressItem).then(res=>{
      this.$message.success('修改成功');
      this.show = false;
      this.$emit('confirm')
    })
  }

}
</script>

<style scoped>

</style>